package com.atx88;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast; 

public class Test2Activity extends Activity {
	TextView textView1;
	Button button1;
	Button button2; 
	Button button3;
	
	SeekBar seekBar1;
	
	boolean inplay=false;
	int miniPlayBufSize;
	
	static final int frequency = 16000;
	static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
	static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
	AudioTrack audioTrack;
	
	//OnSeekBarChangeListener changeListener = null;
	
	short decodes[]=new short[5944]; 
	
	private OnSeekBarChangeListener changeListener = new OnSeekBarChangeListener()
	{

		@Override
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub
			//Toast.makeText(getBaseContext(), String.valueOf(seekBar1.getProgress()), Toast.LENGTH_SHORT).show();
			textView1.setText(String.valueOf(seekBar1.getProgress()));
			
			for(int i=0;i<5944;i++)
			{
				double pi = 3.1415926;
				double angle = i/(double)seekBar1.getProgress()*(2*pi);//
				decodes[i] = (short)(Math.sin(angle) * 0x1000);
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			Toast.makeText(getApplicationContext(), "onStartTrackingTouch",  
			Toast.LENGTH_SHORT).show();  			
		}

		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			Toast.makeText(getApplicationContext(), "onStopTrackingTouch",  
			Toast.LENGTH_SHORT).show();  	
		}
	
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        button1 = (Button) this.findViewById(R.id.button1);
        button2 = (Button) this.findViewById(R.id.button2);
        button3 = (Button) this.findViewById(R.id.button3);
        seekBar1 = (SeekBar) this.findViewById(R.id.seekBar1);
        textView1 = (TextView) this.findViewById(R.id.textView1);
        //OnSeekBarChangeListener changeListener = null;
        seekBar1.setOnSeekBarChangeListener(changeListener);
        seekBar1.setMax(100);
        seekBar1.setProgress(30);
        seekBar1.setSecondaryProgress(80);
        
        final Thread thread = new Thread()
        //final class thread implements Runnable{
        {
        		   public void run()
        		   {
       				miniPlayBufSize = AudioTrack.getMinBufferSize(frequency,
    						channelConfiguration, audioEncoding);
    				
    				audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
    						channelConfiguration, audioEncoding,
    						miniPlayBufSize, AudioTrack.MODE_STREAM);
    				
    				audioTrack.play();
    				
    				for(int i=0;i<5944;i++)
    				{
    					double pi = 3.1415926;
    					double angle = i/(double)64*(2*pi);//
    					decodes[i] = (short)(Math.sin(angle) * 0x100);
    				}
    				/*
    				FileOutputStream fos = null;
    				File sinFile = new File("/sdcard/sin.pcm");
    				try {
    					fos = new FileOutputStream(sinFile);
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				*/
    				while(true)
    				{
    					/*
    					for(int i=0;i<1024;i++)
    					{
    						try {
    							fos.write((int)(decodes[i]));
    						} catch (IOException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}	
    					}
    					*/
    					
    					if(inplay)
    					{
    						audioTrack.write(decodes, 0, decodes.length);
    					}
    				}
    				//return;
        		   }
        };
        
        thread.start();
        
        button3.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(inplay == false)
				{
					inplay = true;
				}
				else
				{
					inplay = false;
					//thread.stop();
				}
			}});
        
        //
        button1.setOnClickListener(new OnClickListener()
        {

			@Override
			public void onClick(View v) {
				Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
			}
        	
        });
        
        
        button2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				miniPlayBufSize = AudioTrack.getMinBufferSize(frequency,
						channelConfiguration, audioEncoding);
				
				audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
						channelConfiguration, audioEncoding,
						miniPlayBufSize, AudioTrack.MODE_STREAM);
				
				audioTrack.play();
				
				for(int i=0;i<5944;i++)
				{
					double pi = 3.1415926;
					double angle = i/(double)64*(2*pi);//
					decodes[i] = (short)(Math.sin(angle) * 0x1000);
				}
				/*
				FileOutputStream fos = null;
				File sinFile = new File("/sdcard/sin.pcm");
				try {
					fos = new FileOutputStream(sinFile);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				*/
				while(inplay)
				{
					/*
					for(int i=0;i<1024;i++)
					{
						try {
							fos.write((int)(decodes[i]));
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}	
					}
					*/
					audioTrack.write(decodes, 0, decodes.length);
				}
			}
        });
    }
}